//
//  Tools.swift
//  Backhome
//
//  Created by vbn on 2018/6/14.
//  Copyright © 2018年 SmartHome. All rights reserved.
//

import Foundation

public enum KNValidate {
    
    case email(_: String)
    case phoneNum(_: String)
    case carNum(_: String)
    case username(_: String)
    case password(_: String)
    case nickname(_: String)
    
    case URL(_: String)
    case IP(_: String)
    
    
    public var isRight: Bool {
        var predicateStr:String!
        var currObject:String!
        switch self {
        case let .email(str):
            predicateStr = "^[A-Z0-9a-z._%+-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$"
            currObject = str
        case let .phoneNum(str):
            predicateStr = "^((13[0-9])|(15[^4,\\D]) |(17[0,0-9])|(18[0,0-9]))\\d{8}$"
            currObject = str
        case let .carNum(str):
            predicateStr = "^[A-Za-z]{1}[A-Za-z_0-9]{5}$"
            currObject = str
        case let .username(str):
            predicateStr = "^[A-Za-z0-9]{6,20}+$"
            currObject = str
        case let .password(str):
            predicateStr = "^[a-zA-Z0-9]{6,20}+$"
            currObject = str
        case let .nickname(str):
            predicateStr = "^[\\u4e00-\\u9fa5]{4,8}$"
            currObject = str
        case let .URL(str):
            predicateStr = "^(https?:\\/\\/)?([\\da-z\\.-]+)\\.([a-z\\.]{2,6})([\\/\\w \\.-]*)*\\/?$"
            currObject = str
        case let .IP(str):
            predicateStr = "^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$"
            currObject = str
        }
        
        let predicate =  NSPredicate(format: "SELF MATCHES %@" ,predicateStr)
        return predicate.evaluate(with: currObject)
    }
}

public func KNSafeParams( params: inout Dictionary<String, String>, key: String, value: String?) {
    if value != nil {
        params.updateValue(value!, forKey: key)
    }
}

public var KNScreenWidth: CGFloat {
    return UIScreen.main.bounds.width
}

public var KNScreenHeight: CGFloat {
    return UIScreen.main.bounds.height
}

public var KNOnePix: CGFloat {
    return 1.0/UIScreen.main.scale
}


public var KNStatusBarHeight: CGFloat {
    return UIApplication.shared.statusBarFrame.height
}

public var  KNNavBarHeight: CGFloat {
    return 44.0
}

public var  KN_topHeight: CGFloat {
    return KNNavBarHeight + KNStatusBarHeight
}

public var KNIPhoneFullScreen: Bool {
    return KNHelper.knisIphoneFullScreen()
}

public func KN_relativeWidth(baseWidth_3x: CGFloat) -> CGFloat {
    return ceil(baseWidth_3x/414 * KNScreenWidth)
}

public func KN_relativeHeight(baseHeight_3x:CGFloat) -> CGFloat {
    return ceil(baseHeight_3x/736 * KNScreenHeight)
}

public func KNDLog<T>(_ info:T, fileName: String = #file, methodName: String = #function, lineNumber: Int = #line, _ showDetail: Bool = true) {
    #if DEBUG
    let file = URL(string: fileName)?.lastPathComponent
    
    if showDetail {
        if let file = file {
            print("\(file) -> \(methodName) [line \(lineNumber)]: ", terminator: "")
        } else {
            print("\(methodName) [line \(lineNumber)]: ", terminator: "")
        }
        
        debugPrint(info)
        
    } else {
        debugPrint(info)
    }
    #endif
}

public func KNNotificationAddObserver(_ observer: Any, _ selector: Selector,_ name: Notification.Name, _ obj: Any?) {
    NotificationCenter.default.addObserver(observer, selector: selector, name: name, object: obj)
    
}

public func KNNotificationPost(_ name: NSNotification.Name, object: Any?) {
    NotificationCenter.default.post(name: name, object: object)
}


public func KNNotificationRemoveObserver(_ observer: Any,_ name: Notification.Name) {
    NotificationCenter.default.removeObserver(observer, name: name, object: nil)
}

public func KNNotificationObserverRemvoeAll(_ observer: Any) {
    NotificationCenter.default.removeObserver(observer)
}
